import _ from 'lodash';
import { ContextMenuTrigger } from 'react-contextmenu';
import { ConsoleSqlOutlined } from '@ant-design/icons';
import { TreeSelect } from 'antd';
const { TreeNode } = TreeSelect;

// 把接口数据转换成前端 menu 数据
export function createMenuData(list) {
  var newLIst = [...list];
  newLIst = newLIst.map((item, i) => {
    // 树组件右键菜单id
    var treeitemRightClickId = 'treeitemRightClick';
    var title = (
      <ContextMenuTrigger
        id={treeitemRightClickId}
        collect={() => ({ ...item })}
      >
        {item.name}
      </ContextMenuTrigger>
    );
    // 菜单icon
    var icon = (
      <ContextMenuTrigger
        id={treeitemRightClickId}
        collect={() => ({ id: item.id })}
      >
        <ConsoleSqlOutlined />
      </ContextMenuTrigger>
    );

    var obj = {
      title: title,
      titleText: item.name,
      key: item.id,
      value: item.id,
      type: 'folder',
      propsData: { ...item },
      children: [],
    };

    // 判断是不是文件类型
    if (item.fileType) {
      obj.type = item.fileType;
      obj.icon = icon;
    }

    // 递归子集
    if (item.children && item.children.length != 0) {
      obj.children = createMenuData(item.children);
    }
    return obj;
  });

  return newLIst;
}

// 把树结构转换成 TreeNode
export function getTreeNodes(list, type, id) {
  var newLIst = [...list];
  return newLIst.map((item, i) => {
    // 禁止移动自己（以及子级）
    if (type == 'mobile' && item.value == id) return;
    var children = [];
    // 递归子集
    if (item.children && item.children.length != 0) {
      children = getTreeNodes(item.children, type, id);
    }
    var Dom = (
      <TreeNode key={item.value} value={item.value} title={item.label}>
        {children}
      </TreeNode>
    );
    return Dom;
  });
}

// 转换数据格式 符合TreeSelect结构
export function transformTreeData(list) {
  var newLIst = [...list];
  return newLIst.map((item, i) => {
    var obj = {
      value: item.id,
      label: item.name,
      children: [],
    };
    // 递归子集
    if (item.children && item.children.length != 0) {
      obj.children = transformTreeData(item.children);
    }
    return obj;
  });
}
